package city.spring.controller;

import city.spring.modules.system.entity.PositionsEntity;
import city.spring.modules.system.service.PositionsService;
import city.spring.modules.system.vo.PositionsEntityVO;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：岗位
 *
 * @author pxm
 * @date 2020-08-06 15:48:00
 */
@RestController
@RequestMapping("/positions")
public class PositionsController extends ApiController {
    private final PositionsService positionsService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<PositionsEntity, ?>> entityOrderFields;

    public PositionsController(PositionsService positionsService) {
        this.positionsService = positionsService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(PositionsEntity::getId);
        entityOrderFields.add(PositionsEntity::getOrgId);
        entityOrderFields.add(PositionsEntity::getName);
        entityOrderFields.add(PositionsEntity::getIsLeader);
        entityOrderFields.add(PositionsEntity::getMarks);
        entityOrderFields.add(PositionsEntity::getGmtCreate);
        entityOrderFields.add(PositionsEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>岗位</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, PositionsEntity entity) {
        LambdaQueryChainWrapper<PositionsEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>岗位</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, PositionsEntityVO entity) {
        QueryWrapper<? extends PositionsEntity> queryWrapper = buildQuery(entity);
        return success(positionsService.listPage(MyBatisUtils.toPage(pageable), queryWrapper));

    }

    /**
     * 获取部门下的 <strong>岗位</strong> 列表
     */
    @GetMapping("{orgId}/listUnderOrg")
    public Object listUnderOrg(@PageableDefault Pageable pageable, PositionsEntity entity, @PathVariable String orgId) {
        LambdaQueryChainWrapper<PositionsEntity> lambdaQuery = buildLambdaQuery(entity);
        lambdaQuery.eq(PositionsEntity::getOrgId, orgId);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 获取一个 <strong>岗位</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        Page<PositionsEntityVO> page = new Page<>();
        QueryWrapper<PositionsEntityVO> wrapper = new QueryWrapper<>();
        wrapper.eq("t1.id", id);
        IPage<PositionsEntityVO> iPage = positionsService.listPage(page, wrapper);
        List<PositionsEntityVO> records = iPage.getRecords();
        if (records.isEmpty()) {
            return success(null);
        }
        return success(records.get(0));

    }

    /**
     * 添加一个 <strong>岗位</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody PositionsEntity entity) {
        positionsService.savePositions(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>岗位</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody PositionsEntity entity) {
        entity.setId(id);
        positionsService.updatePositions(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>岗位</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        positionsService.deletePositions(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>岗位</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        positionsService.deletePositions(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<PositionsEntity> buildLambdaQuery(PositionsEntity entity) {
        LambdaQueryChainWrapper<PositionsEntity> lambdaQuery = positionsService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), PositionsEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOrgId()), PositionsEntity::getOrgId, entity.getOrgId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getName()), PositionsEntity::getName, entity.getName());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getIsLeader()), PositionsEntity::getIsLeader, entity.getIsLeader());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getMarks()), PositionsEntity::getMarks, entity.getMarks());
        lambdaQuery.eq(entity.getGmtCreate() != null, PositionsEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, PositionsEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<? extends PositionsEntity> buildQuery(PositionsEntityVO entity) {
        QueryWrapper<? extends PositionsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "t1.id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getOrgId()), "t1.org_id", entity.getOrgId());
        wrapper.like(StringUtils.isNotBlank(entity.getName()), "t1.name", entity.getName());
        wrapper.like(StringUtils.isNotBlank(entity.getOrgName()), "t2.title", entity.getOrgName());
        wrapper.like(StringUtils.isNotBlank(entity.getIsLeader()), "t1.is_leader", entity.getIsLeader());
        wrapper.like(StringUtils.isNotBlank(entity.getMarks()), "t1.marks", entity.getMarks());
        wrapper.eq(entity.getGmtCreate() != null, "t1.gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "t1.gmt_modified", entity.getGmtModified());
        return wrapper;
    }

    /**
     * 获取岗位名称为药师的岗位信息
     *
     * @return
     */
    @GetMapping("/getPharmacist")
    public Object getPharmacist() {
        return success(positionsService.getPharmacist());
    }

    /**
     * 获取岗位名称为医生的岗位信息
     *
     * @return
     */
    @GetMapping("/getDoctor")
    public Object getDoctor() {
        return success(positionsService.getDoctor());
    }


}
